/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Session;

import Entity.Contrathabitat;
import Entity.Client;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.ejb.Local;

/**
 *
 * @author Gautch
 */
@Stateless
public class ContratHabitatSession implements ContratHabitatSessionLocal {

    @javax.persistence.PersistenceContext(unitName = "Assurance-ejbPU")
    private EntityManager em;

    @Override
    public Contrathabitat getContrat(String numcontrat) {
        Integer num = (Integer) Integer.parseInt(numcontrat);
        Query q = em.createNamedQuery("Contrathabitat.findByNumcontrathabitat");
        q.setParameter("numcontrathabitat", num);
        Contrathabitat c = (Contrathabitat) q.getSingleResult();
        return c;

    }

    @Override
    public List<Contrathabitat> recupContratHabitatByNumClient(String numClient) {

        System.out.println("lalal");

        List<Contrathabitat> listContrat = new ArrayList<Contrathabitat>();
        System.out.println("lololo");

        Query recup = em.createNamedQuery("Contrathabitat.findByNumeroclient");

        System.out.println("je parse " + numClient);

        int num = Integer.parseInt(numClient);
//        Integer numClientInteger = (Integer) num;
//        BigInteger bi = BigInteger.valueOf(numClientInteger.intValue());
        System.out.println("j'ai parsé");

        recup.setParameter("numeroclient", num);
        System.out.println("aaaaaaaaaaaaaaa");

        listContrat = recup.getResultList();

        return listContrat;
    }

    @Override
    public List<Contrathabitat> recupContratAutoAValider() {

        List<Contrathabitat> listContrat = new ArrayList<Contrathabitat>();
        Query recup = em.createNamedQuery("Contrathabitat.findByStatut");
        recup.setParameter("statut", "aValiderHab");
        listContrat = recup.getResultList();
        return listContrat;

    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public void traitementContratHabRedac(String numRedacteur, String newEtat, String numContrat) {
        
        System.out.println("Je rentre dans traitementContratHabRedac");
        Query find = em.createNamedQuery("Contrathabitat.findByNumcontrathabitat");
        System.out.println("Query exécutée, numContrat: " + numContrat);

        int num = Integer.parseInt(numContrat);



        Integer numerocontratInt = (Integer) num;
                System.out.println("Je parse " + numerocontratInt);

        find.setParameter("numcontrathabitat", numerocontratInt);
        System.out.println("test1");

        Contrathabitat ca = (Contrathabitat) find.getSingleResult();
        // TODO rajouter redacteur dans la BD de contratAuto
        //s.setNumredacteur(numRedacteur);
        ca.setStatut(newEtat);
        em.persist(ca);

    }

    @Override
    public int[] calculDevis(int surface, boolean incendie, boolean vol, boolean degatEau, boolean bienPrecieux){
        int categorie = cat(surface);
        int evaluation[] = new int[3];
        evaluation[0] = 0;
        evaluation[1] = 0;
        evaluation[2] = categorie;
        switch (categorie) {
            case 1:
                evaluation[0] += 260;
                if (incendie)evaluation[0] += 40;
                
                if (vol) evaluation[0] += 50;
                
                if(degatEau) evaluation[0] += 70;
                
                if (bienPrecieux) evaluation[0] += 130;
                
                evaluation[1] = 70;
                break;
            case 2:
                evaluation[0] += 360;
                if (incendie)evaluation[0] += 50;
                
                if (vol) evaluation[0] += 60;
                
                if(degatEau) evaluation[0] += 80;
                
                if (bienPrecieux) evaluation[0] += 150;
                
                evaluation[1] = 100;
                break;
            case 3:
                evaluation[0] += 460;
                if (incendie)evaluation[0] += 60;
                
                if (vol) evaluation[0] += 70;
                
                if(degatEau) evaluation[0] += 90;
                
                if (bienPrecieux) evaluation[0] += 170;
                
                evaluation[1] = 150;
                break;
            default:
                evaluation[0] = -1;
                evaluation[1] = -1;
                break;
        }
        return evaluation;

    }
    
    public static int cat(int surface) {
        int categorie = -1;
        if(surface <40) categorie = 1;
        else if(surface >=40 && surface <= 150) categorie= 2;
        else categorie = 3;
        return categorie;
    }
    
    public boolean souscriptionHabitat(int surface, boolean incendie, boolean vol,
            boolean degatEau, boolean bienPrecieux, String adresse, String code, int categorie, 
            int prix, int franchise, String login){
        if(adresse == null || login == null) return false;
        Query q = em.createNamedQuery("Client.findByEmail");
        q.setParameter("email", login);
        Client c = (Client) q.getSingleResult();
        int numeroClient = c.getIdclient();
        Contrathabitat ch = new Contrathabitat(surface, incendie, vol,
            degatEau, bienPrecieux, adresse, code, categorie, 
            prix, franchise, numeroClient);
        em.persist(ch);
        return true;
    }
    
    public boolean testExistence(String numeroContrat){
        Query q = em.createNamedQuery("Contrathabitat.findByNumcontrathabitat");
        Integer num = (Integer) Integer.parseInt(numeroContrat);
        q.setParameter("numcontrathabitat", num);
        Contrathabitat c = (Contrathabitat) q.getSingleResult();
        if (!c.equals(null)) {
            return true;
        }

        return false;

    }
    
    @Override
    public boolean supprimer(String numero){
        Contrathabitat ch = getContrat(numero);
        em.remove(em.merge(ch));
        
        return true;
    }
}
